//
//  GYMineCollectCell.m
//  GYDemo
//
//  Created by mac on 2018/6/21.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYMineCollectCell.h"
#import "Head.h"

@implementation GYMineCollectCell
#pragma mark - getter and setter
-(NSInteger)column
{
    if (_column <= 0) {
        _column = 4;
    }
    return _column;
}
-(CGFloat)height
{
    NSArray *items = _model.items;
    CGFloat width = ScaleScreenWidth / self.column;
    CGFloat height = width;
    CGFloat fheight = (items.count / _column + (items.count % _column > 0 ? 1 : 0)) *height;
    return fheight;
}
-(void)setModel:(GYTableSectionViewModel *)model
{
    _model = model;
    [self.contentView removeAllSubviews];
    NSArray *items = model.items;
    CGFloat width = ScaleScreenWidth / self.column;
    CGFloat height = width;
    self.height = (items.count / _column + (items.count % _column > 0 ? 1 : 0)) *height;
    for (NSInteger i = 0; i < items.count; i++) {
        GYUserImageTitleCellModel *cellmodel = items[i];
        UIView *target = [UIView new];
        target.backgroundColor = Main_Color_White;
        target.width = width;
        target.height = height;
        target.x = (i % _column) * width;
        target.y = (i / _column) * height;
        [self.contentView addSubview:target];
        
        UIImage *img = [UIImage imageNamed:cellmodel.imgName];
        UIImageView *_titleImgV = [UIImageView new];
        _titleImgV.image = img;
        CGFloat scale = (width * 0.382) / img.size.width;
        if (img.size.height > img.size.width) {
            scale = (height * 0.382) / img.size.height;
        }
        _titleImgV.width = img.size.width * scale;
        _titleImgV.height = img.size.height * scale;
        _titleImgV.centerX = width / 2;
        _titleImgV.centerY = height * 0.382;
//        _titleImgV.centerY = height * 0.382 - _titleImgV.height / 2;
        [target addSubview:_titleImgV];
        
        UILabel *_titleLab = [UILabel new];
        _titleLab.width = width;
        _titleLab.height = WFCGFloatY(16);
        _titleLab.centerY = height * 0.618 + _titleLab.height / 2;
//        _titleLab.centerY = height * 0.618;
        _titleImgV.centerY = height * 0.382 - _titleImgV.height / 2 + _titleLab.height / 2;
        _titleLab.font = SYSTEMFONT(14);
        _titleLab.textColor = Main_Color_Gray;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = cellmodel.title;
        [target addSubview:_titleLab];
        @WeakSelf;
        [target addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            if (selfp.callback) {
                selfp.callback(cellmodel);
            }
        }];
    }
}
#pragma mark - event
#pragma mark - method
#pragma mark - life
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
